package net.wisedream.littletools.jff.util;

import org.apache.http.client.HttpClient;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.BasicClientConnectionManager;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.conn.SchemeRegistryFactory;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.SyncBasicHttpParams;

/**
 * HttpClient工厂
 *
 * @author zxp
 * @Created Feb 14, 2014
 */
public class HttpClientFactory {
	/**
	 * 
	 * @param isMultiRequest
	 *            是否支持多线程
	 * @param connectionTimeout
	 *            建立连接超时时间(毫秒)
	 * @param socketTimeout
	 *            等待数据超时时间(毫秒)
	 * @return
	 */
	public static HttpClient getHttpClient(boolean isMultiRequest,
			int connectionTimeout, int socketTimeout) {
		SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();
		ClientConnectionManager cm = isMultiRequest ? new PoolingClientConnectionManager(
				schemeRegistry) : new BasicClientConnectionManager(
				schemeRegistry);
		HttpParams params = isMultiRequest ? new SyncBasicHttpParams()
				: new BasicHttpParams();
		HttpClientParams.setCookiePolicy(params,
				CookiePolicy.BROWSER_COMPATIBILITY);
		HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
		HttpConnectionParams.setSoTimeout(params, socketTimeout);
		return new DefaultHttpClient(cm, params);
	}

}
